package lumingweihua.future.cn.lumingweihua.ui.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Lists;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.baselibgxh.utils.CommonUtils;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.ui.BaseAppFragment;
import lumingweihua.future.cn.lumingweihua.ui.order.domain.SupplyData;

/**
 * Author       zhusx
 * Email        327270607@qq.com
 * Created      2018/3/15 16:49
 */

public class SupplyGoodsInfoFragment extends BaseAppFragment {
    @BindView(R.id.layout_content)
    ViewGroup contentLayout;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_fromName)
    TextView tvFromName;
    @BindView(R.id.tv_fromCity)
    TextView tvFromCity;
    @BindView(R.id.tv_rough_weight)
    TextView tvRoughWeight;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.layout_upload)
    LinearLayout layoutUpload;

    String good_num;
    SupplyData data;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_order_goods, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        LoadData<SupplyData> loadData = new LoadData<>(LoadData.Api.货主订单订单详情, this);
        loadData._setOnLoadingListener(new LoadingHelper<SupplyData>(contentLayout, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<SupplyData> data) {
                initView(data.getData());
            }
        });
        loadData._loadData(good_num);
    }

    private void initView(SupplyData data) {
        this.data = data;
        tvNum.setText(data.good_num);
        tvFromName.setText(data.loading);
        tvFromCity.setText(data.loading_address);
        tvRoughWeight.setText(data.mileage);
        tvType.setText(data.type);
        tvDate.setText(data.use_time);
        tvMessage.setText(data.remark);
        layoutUpload.removeAllViews();
        if (!_Lists.isEmpty(data.unload)) {
            for (int i = 0; i < data.unload.size(); i++) {
                View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_item_order, layoutUpload, true);
                ((TextView) itemView.findViewById(R.id.tv_toCity)).setText(data.unload_address.get(i));
                ((TextView) itemView.findViewById(R.id.tv_toName)).setText(data.unload.get(i));
                final String lat = data.lats.get(i);
                final String lon = data.lons.get(i);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CommonUtils.navi(view.getContext(), lat, lon, "");
                    }
                });
            }
        }
    }

    public void setId(String good_num) {
        this.good_num = good_num;
    }

    @OnClick({R.id.layout_address})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_address:
                CommonUtils.navi(v.getContext(), data.lat, data.lon, "");
                break;
        }
    }
}
